import { azLogin } from '../request/home'

export function login() {  
  wx.login({
    success: res => {
      // 发送 res.code 到后台换取 openId, sessionKey, unionId
      console.log("code--" + res.code)
      loginRequest(res.code)
    }
  });
}

function loginRequest(code) {
  var params = {
      'code':code,
  }
  azLogin(params).then((res) => {
    //token保存到本地
    if (res.data.token != null){
      wx.setStorageSync('token', "Bearer " + res.data.token)
      wx.setStorageSync('userId', res.data.userInfo.id)
    }
  }).catch((err) => {
    console.log("失败---",err)
  });
}